package ua.org.extjsbuilder.parser.runtime;

import ua.org.extjsbuilder.parser.IInstruction;
import ua.org.extjsbuilder.parser.runtime.instructions.BinaryOperator;
import ua.org.extjsbuilder.parser.runtime.instructions.Cycle;
import ua.org.extjsbuilder.parser.runtime.instructions.Declare;
import ua.org.extjsbuilder.parser.runtime.instructions.Empty;
import ua.org.extjsbuilder.parser.runtime.instructions.GetEntity;
import ua.org.extjsbuilder.parser.runtime.instructions.UnaryOperator;

public class CyclesInterpreterTestCase extends AbstractInterpreterTestCase {

	@Override
	protected String getMockPkg() {
		return "ua/org/extjsbuilder/parser/runtime/cycles/";
	}
	
	public void testWhile() throws Exception{
		IInstruction cyc = parseFile("while1.js", Declare.class, Cycle.class).get(1);
		assertEquals(Cycle.class,cyc.getClass());
		Cycle cycle = (Cycle)cyc;
		assertEquals(Cycle.WHILE,cycle.getName());
		assertEquals(GetEntity.class,cycle.getCondition().getClass());
		assertInstructions(cycle.getMainBlock(), Declare.class);
		assertNull(cycle.getDeclaration());
		assertNull(cycle.getIncrement());
		
		cyc = parseFile("while2.js", Declare.class, Cycle.class).get(1);
		assertEquals(Cycle.class,cyc.getClass());
		cycle = (Cycle)cyc;
		assertEquals(Cycle.WHILE,cycle.getName());
		assertEquals(GetEntity.class,cycle.getCondition().getClass());
		assertInstructions(cycle.getMainBlock(), Declare.class);
		assertNull(cycle.getDeclaration());
		assertNull(cycle.getIncrement());
		
		cyc = parseFile("while3.js", Declare.class, Cycle.class).get(1);
		assertEquals(Cycle.class,cyc.getClass());
		cycle = (Cycle)cyc;
		assertEquals(Cycle.WHILE,cycle.getName());
		assertEquals(GetEntity.class,cycle.getCondition().getClass());
		assertInstructions(cycle.getMainBlock(), Declare.class);
		assertNull(cycle.getDeclaration());
		assertNull(cycle.getIncrement());
	}
	
	public void testFor() throws Exception{
		IInstruction cyc = parseFile("for1.js", Cycle.class).get(0);
		assertEquals(Cycle.class,cyc.getClass());
		Cycle cycle = (Cycle)cyc;
		assertEquals(Cycle.FOR,cycle.getName());
		assertEquals(GetEntity.class,cycle.getCondition().getClass());
		assertInstructions(cycle.getMainBlock(), Declare.class, Declare.class);
		
		assertEquals(BinaryOperator.class,cycle.getDeclaration().getClass());
		BinaryOperator dec = (BinaryOperator)cycle.getDeclaration();
		assertEquals(BinaryOperator.EQ, dec.getName());
		
		assertEquals(UnaryOperator.class,cycle.getIncrement().getClass());
		UnaryOperator un = (UnaryOperator)cycle.getIncrement();
		assertEquals(UnaryOperator.DECREMENT, un.getName());
		
		cyc = parseFile("for2.js", Cycle.class).get(0);
		
		assertEquals(Cycle.class,cyc.getClass());
		cycle = (Cycle)cyc;
		assertEquals(Cycle.FOR,cycle.getName());
		assertEquals(GetEntity.class,cycle.getCondition().getClass());
		assertInstructions(cycle.getMainBlock(), Declare.class, Declare.class);
		
		assertEquals(BinaryOperator.class,cycle.getDeclaration().getClass());
		dec = (BinaryOperator)cycle.getDeclaration();
		assertEquals(BinaryOperator.EQ, dec.getName());
		
		assertEquals(UnaryOperator.class,cycle.getIncrement().getClass());
		un = (UnaryOperator)cycle.getIncrement();
		assertEquals(UnaryOperator.INCREMENT, un.getName());
		
		cyc = parseFile("for3.js", Cycle.class).get(0);
		
		assertEquals(Cycle.class,cyc.getClass());
		cycle = (Cycle)cyc;
		assertEquals(Cycle.FOR,cycle.getName());
		
		assertEquals(Empty.class,cycle.getCondition().getClass());
		assertEquals(Empty.class,cycle.getDeclaration().getClass());
		assertEquals(Empty.class,cycle.getIncrement().getClass());
		
		assertInstructions(cycle.getMainBlock(), Declare.class, Declare.class);
		
		cyc = parseFile("for4.js", Cycle.class).get(0);
		assertEquals(Cycle.class,cyc.getClass());
		cycle = (Cycle)cyc;
		assertEquals(Cycle.FOR,cycle.getName());
		assertEquals(GetEntity.class,cycle.getCondition().getClass());
		assertInstructions(cycle.getMainBlock(), Declare.class);
		
		assertEquals(BinaryOperator.class,cycle.getDeclaration().getClass());
		dec = (BinaryOperator)cycle.getDeclaration();
		assertEquals(BinaryOperator.EQ, dec.getName());
		
		assertEquals(UnaryOperator.class,cycle.getIncrement().getClass());
		un = (UnaryOperator)cycle.getIncrement();
		assertEquals(UnaryOperator.DECREMENT, un.getName());
	}
	
	public void testDo() throws Exception{
		IInstruction cyc = parseFile("do1.js", Declare.class, Cycle.class).get(1);
		assertEquals(Cycle.class,cyc.getClass());
		Cycle cycle = (Cycle)cyc;
		assertEquals(Cycle.DO,cycle.getName());
		assertEquals(GetEntity.class,cycle.getCondition().getClass());
		assertInstructions(cycle.getMainBlock(), Declare.class);
		assertNull(cycle.getDeclaration());
		assertNull(cycle.getIncrement());
		
		cyc = parseFile("do2.js", Declare.class, Cycle.class).get(1);
		assertEquals(Cycle.class,cyc.getClass());
		cycle = (Cycle)cyc;
		assertEquals(Cycle.DO,cycle.getName());
		assertEquals(GetEntity.class,cycle.getCondition().getClass());
		assertInstructions(cycle.getMainBlock(), Declare.class);
		assertNull(cycle.getDeclaration());
		assertNull(cycle.getIncrement());
		
		cyc = parseFile("do3.js", Declare.class, Cycle.class).get(1);
		assertEquals(Cycle.class,cyc.getClass());
		cycle = (Cycle)cyc;
		assertEquals(Cycle.DO,cycle.getName());
		assertEquals(GetEntity.class,cycle.getCondition().getClass());
		assertInstructions(cycle.getMainBlock(), Declare.class);
		assertNull(cycle.getDeclaration());
		assertNull(cycle.getIncrement());
	}
}
